/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.dao.jpa;

import com.supinfo.youfood.dao.EmployeDao;
import com.supinfo.youfood.entity.Employe;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Michael
 */
@Stateless
public class JpaEmployeDao implements EmployeDao{
    
    @PersistenceContext
    EntityManager em;
    
    @Override
    public Employe login (Employe e){
            String query = "SELECT e "+
                       "FROM Employe e "+
                       "WHERE e.code_acces = :code_acces";
        
            Query result = em.createQuery(query);
            result.setParameter("code_acces", e.getCode_acces());
            if(result.getResultList().size() == 1)
                return (Employe)result.getSingleResult();
            else
                return null;
    }

    @Override
    public Employe addEmploye(Employe e) {
        em.persist(e);
        return e;
    }

    @Override
    public List<Employe> getAllEmploye() {
        String query = "SELECT e "+
                        "FROM Employe e";
        Query result = em.createQuery(query);
        return (List<Employe>)result.getResultList();
    }

    @Override
    public Employe findEmployeById(Long id) {
        return em.find(Employe.class, id);
    }

    @Override
    public Employe refreshEmploye(Employe e) {
        return em.merge(e);
    }
}
